/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Datos;

/**
 *
 * @author luis.hidalgo
 */
import java.io.PrintStream;
import java.sql.*;

public class ConexionMysql
{

    private Connection con;
    private Statement st;
    private ResultSet rs;

    public ConexionMysql(String usu, String clave)
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch(Exception e)
        {
            System.out.println("No se ha podido cargar el Driver JDBC-ODBC");
        }
        try
        {
            con = DriverManager.getConnection("jdbc:mysql://localhost/dbinventario", usu, clave);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        try
        {
            st = con.createStatement();
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    }

    public ResultSet Cons(String query)
    {
        try
        {
            rs = st.executeQuery(query);
        }
        catch(Exception e)
        {
            e.getCause();
        }
        return rs;
    }

    public void Insertar(String query)
    {
        try
        {
            st.executeUpdate(query);
        }
        catch(Exception e)
        {
            e.getCause();
        }
    }

    public void desconectar()
        throws Exception
    {
        if(st != null)
        {
            try
            {
                st.close();
            }
            catch(SQLException sqlexception) { }
        }
        if(con != null)
        {
            try
            {
                con.close();
            }
            catch(SQLException sqlexception1) { }
        }
    }
}
